/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;


import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.Teachchapter;
import me.zhengjie.modules.system.domain.TeachchapterDo;
import me.zhengjie.modules.system.domain.TeachsubsectionDo;
import me.zhengjie.modules.system.service.TeachchapterService;
import me.zhengjie.modules.system.domain.vo.TeachchapterQueryCriteria;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.List;

import me.zhengjie.modules.system.service.TeachsubsectionService;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

import static me.zhengjie.modules.system.domain.TeachchapterDo.convertFromTypeA;
import static me.zhengjie.utils.TreeDataUtil.*;

/**
* @author shitou
* @date 2024-04-21
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "章节管理")
@RequestMapping("/api/teachchapter")
public class TeachchapterController {

    private final TeachchapterService teachchapterService;

    private final TeachsubsectionService teachsubsectionService;


    @GetMapping("/queryTeachchapterMsg")
    @Log("查询教材小节")
    public ResponseEntity<Teachchapter> queryTeachchapterMsg(@RequestParam(name = "teachchapterid", required = true) Integer teachchapterid){
        return new ResponseEntity<>(teachchapterService.queryTeachchapterMsg(teachchapterid),HttpStatus.OK);
    }
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('teachchapter:list')")
    public void exportTeachchapter(HttpServletResponse response, TeachchapterQueryCriteria criteria) throws IOException {
        teachchapterService.download(teachchapterService.queryAll(criteria), response);
    }

    @GetMapping("/queryChapterList")
    public ResponseEntity<List<TeachchapterDo>> queryChapterList(Integer teachid){
        List<Teachchapter> list1 = teachchapterService.queryChapterList(teachid);
        List<TeachchapterDo> list3 = new ArrayList<>();
        for(Teachchapter item : list1){
            TeachchapterDo item2 = convertFromTypeA(item);
            list3.add(item2);
        }

        List<TeachsubsectionDo> list2 = teachsubsectionService.querySubsectionList(teachid);

        List<Map<String,Object>> list4 = new ArrayList<>();
        for(TeachsubsectionDo teachsubsectionDo :list2){
            Map<String, Object> map = convertObjectToMap(teachsubsectionDo);
            map.put("id",teachsubsectionDo.getId());
            map.put("children",new ArrayList<>());
            list4.add(map);
        }

        List<Map<String,Object>> list5 = list4.stream().filter(m -> (m.get("parentid") + "").equals("0")).peek(
                (m) -> m.put("children", getChildren(m,list4))
        ).collect(Collectors.toList());
//        List<Map<String,Object>> objectList = getResultList(list5);

        for(TeachchapterDo teachchapterDo:list3){
            List<Map<String,Object>> currentList = new ArrayList<>();
            for(Map<String,Object> object :list5){
                if(teachchapterDo.getId() == object.get("teachchapterid")){
                    currentList.add(object);

                }
            }
            teachchapterDo.setChildren(currentList);
        }

        return new ResponseEntity<>(list3,HttpStatus.OK);
    }

    public static List<Map<String,Object>> getChildren(Map<String,Object> root, List<Map<String,Object>> all){
        List<Map<String,Object>> list1 = all.stream().filter(m -> Objects.equals(m.get("parentid"),root.get("id"))).peek(
                (m) -> m.put("children",(getChildren(m,all)))
        ).collect(Collectors.toList());
        return list1;
    }
    @GetMapping
    @Log("查询章节")
    @ApiOperation("查询章节")
//    @PreAuthorize("@el.check('teachchapter:list')")
    public ResponseEntity<PageResult<Teachchapter>> queryTeachchapter(TeachchapterQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(teachchapterService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增章节")
    @ApiOperation("新增章节")
//    @PreAuthorize("@el.check('teachchapter:add')")
    public ResponseEntity<Long> createTeachchapter(@Validated @RequestBody Teachchapter resources){

        return new ResponseEntity<>(teachchapterService.create(resources),HttpStatus.OK);
    }

    @PutMapping
    @Log("修改章节")
    @ApiOperation("修改章节")
//    @PreAuthorize("@el.check('teachchapter:edit')")
    public ResponseEntity<Long> updateTeachchapter(@Validated @RequestBody Teachchapter resources){

        return new ResponseEntity<>(teachchapterService.update(resources),HttpStatus.OK);
    }

    @DeleteMapping
    @Log("删除章节")
    @ApiOperation("删除章节")
//    @PreAuthorize("@el.check('teachchapter:del')")
    public ResponseEntity<Object> deleteTeachchapter(@RequestBody List<Integer> ids) {

        teachchapterService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}